home *** CD-ROM | disk | FTP | other *** search
- Path: news.th-darmstadt.de!news
- From: Enno Sandner <enno@intellektik.informatik.th-darmstadt.de>
- Newsgroups: comp.lang.c++
- Subject: Re: IsA v/s HasA problem
- Date: Fri, 12 Jan 1996 09:18:47 +0100
- Organization: Fachbereich Informatik, TH Darmstadt
- Message-ID: <30F61967.167EB0E7@intellektik.informatik.th-darmstadt.de>
- References: <30F52C66.851@bangate.compaq.com>
- NNTP-Posting-Host: kitz.intellektik.informatik.th-darmstadt.de
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0b4 (X11; I; SunOS 4.1.3 sun4m)
-
- Saurabh Dixit wrote:
- >
- > Hi!
- >
- > I always had no trouble distinguishing between the two...
- >
- > But now, I for a specific case, I fail to see the repercussions of
- > using the wrong relationship between objects.
- >
- > This is my scenario.
- > I have an object of type Base. This is an abstract class.
- > From Base I derive several different objects.
- > I want to maintain a list of such objects for which I have a class
- > Collection that encapsulates a typed array of Base objects.
- >
- > I now want to define an object - let's call it Duh - that maintains a
- > collection of Base objects. Duh objects are entities on their own and could
- > have other attributes, BUT NEVER ANOTHER COLLECTION.
- >
- > From what I gather, I am then leaning towards a HasA relationship, i.e.
- > class Collection
- > {
- > public:
- > BOOL Add (Base* b);
- > };
- > class Duh
- > {
- > private:
- > Collection c;
- > };
- >
- > Obviously if I was going to have 2 or more collections, then I would have had
- > to have a HasA relationship. But as said earlier, this is not the case!
- > Also, I want to have some operations common between Collection and Duh.
- > e.g. I can Add() a Base (derived!) object to a Collection and I also want
- > to Add() an object to Duh. In a HasA relationship I would simply
- > implemenent this as follows.
- > BOOL Duh::Add (Base* b)
- > {
- > // simply pass on to Collection method
- > return c.Add (b);
- > }
- > However for an IsA relationship, I would simply do
- > class Duh : public Collection
- > {
- > };
- > and I am done.
- >
- > Am I wasting my time too much thinking about this? Regardless of code
- > implementation, from the logical perspective, what is the correct object-
- > oriented solution for my problem, HasA or IsA?
- >
-
- The first thing you should think about, if you are planning to let 'Duh'
- and 'Collection' form an ISA relationsship:
-
- is it reasonable to regard 'Duh' as specialization of a 'Collection'
- or do you just 'borrow' the functionality.
-
- For example you should not
- o narrow the interface
- o redefine functions in way that violates the assumptions made in the
- Collection class.
-
- If 'Duh' is planned to be a collection with some additional behavior it's
- reasonable to derive it public from 'Collection'.
- Otherwise the HasA relationsship seems to be the better choice.
-
- Enno
-